package com.wnx.job.modules.pms.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.modules.pms.dto.EmailParam;
import com.wnx.job.modules.pms.mapper.PmsEmailMapper;
import com.wnx.job.modules.pms.model.PmsEmail;
import com.wnx.job.modules.pms.service.PmsEmailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class PmsEmailServiceImpl extends ServiceImpl<PmsEmailMapper, PmsEmail> implements PmsEmailService {
    @Resource
    private PmsEmailMapper emailMapper;
    @Resource
    private JavaMailSenderImpl mailSender;

    /**
     * 获取发给这个学生的邮件
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<EmailParam> getStudentEmail(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<EmailParam>  emailParams = emailMapper.getStudentEmail(queryPageBean.getQueryParams());
        return PageResult.restPage(emailParams);
    }

    /**
     * 发送邮件
     * @param mail
     */
    @Override
    public void sendMail(PmsEmail mail) {
        checkMail(mail);
        try {
            sendMimeMail(mail);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        emailMapper.insert(mail);
    }

    /***
     * 查看公司邮件信息
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<EmailParam> getCompanyEmail(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<EmailParam>  emailParams = emailMapper.getCompanyEmail(queryPageBean.getQueryParams());
        return PageResult.restPage(emailParams);
    }

    /**
     * 检测邮件
     * @param email
     */
    private void checkMail(PmsEmail email) {
        if (StringUtils.isEmpty(email.getToPeople())) {
            Asserts.fail("邮件收信人不能为空");
        }
        if (StringUtils.isEmpty(email.getSubject())) {
            Asserts.fail("邮件主题不能为空");
        }
        if (StringUtils.isEmpty(email.getText())) {
            Asserts.fail("邮件内容不能为空");
        }
    }
    /**
     * 发送邮件服务
     * @param email
     * @throws MessagingException
     */
    private void sendMimeMail(PmsEmail email) throws MessagingException {
        //构造消息邮件帮助类 //true表示支持复杂类
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);


        //设置邮件相关信息
        messageHelper.setFrom(email.getFromPeople());
        messageHelper.setTo(email.getToPeople().split(","));
        messageHelper.setSubject(email.getSubject());
        messageHelper.setText(email.getText());

        //设置发送时间
        if (email.getSentDate() == null) {
            email.setSentDate(new Date());
            messageHelper.setSentDate(email.getSentDate());
        }

        //进行发送邮件
        mailSender.send(messageHelper.getMimeMessage());
        email.setStatus(Const.STATUS_ON);
    }


}
